#ifndef COMPONENTS_COMMON_H
#define COMPONENTS_COMMON_H

#pragma once

#include <string>
#include <stdint.h>
// 使用标准库字面量
using namespace std::literals;

/**
 * 涉及到网络通信请使用 lexx 和 bexx 类型
 * le 代表主机字节序
 * be 代表网络字节序
 */
using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using le16 = uint16_t;
using be16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using le32 = uint32_t;
using be32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using le64 = uint64_t;
using be64 = uint64_t;
/** 设置 IS_DEBUG 帮助判断是否 debug 状态 */
#ifdef NDEBUG
#define IS_DEBUG false
#else
#define IS_DEBUG true
#endif

/**
 * @brief 释放资源帮助类
 *
 * @tparam T 释放资源时回调的函数
 */
template <std::invocable<> T>
struct ScopeGuard {
    T callback;
    ~ScopeGuard() { callback(); }
};
template <typename T>
ScopeGuard(T) -> ScopeGuard<T>;
#define SCOPE_GUARD_VAR_JOIN(arg1, arg2) SCOPE_GUARD_VAR_JOIN2(arg1, arg2)
#define SCOPE_GUARD_VAR_JOIN2(arg1, arg2) arg1 ## arg2
#define on_scope_exit(...) auto SCOPE_GUARD_VAR_JOIN(_SCOPE_GUARD, __LINE__) = ScopeGuard(__VA_ARGS__)

#endif
